package com.sskj.wallet.ui.activity;


import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.LazyLoaders;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.ValueScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.WalletBean;
import com.sskj.lib.model.room.WalletBeanViewModel;
import com.sskj.lib.util.ETHUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.CreateBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.CreateWalletActivityPresenter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.WALLET_CREATE_WALLET)
public class CreateWalletActivity extends BaseActivity<CreateWalletActivityPresenter> {
    @BindView(R2.id.etName)
    EditText etName;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R2.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R2.id.checkbox)
    CheckBox checkbox;
    @BindView(R2.id.tvAgreement)
    TextView tvAgreement;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private boolean isSeeAgain;
    private boolean isSee;
    private AndroidNextInputs inputs;
    @Inject
    WalletBeanViewModel walletBeanViewModel;
    @Autowired
    boolean isFirst=true;
    @Autowired
    CoinEnum coinEnum=CoinEnum.ETH;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_create_wallet;
    }

    @Override
    public CreateWalletActivityPresenter getPresenter() {
        return new CreateWalletActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.wallet_createWalletActivity1_j));
        DaggerUserDataComponent.create().inject(this);
        inputs = new AndroidNextInputs();
        WidgetAccess widgetAccess = new WidgetAccess(this);
        LazyLoaders lazyLoaders = new LazyLoaders(this);
        inputs.clear();
        inputs
                .add(widgetAccess.findEditText(R.id.etName), StaticScheme.required())
                .add(widgetAccess.findEditText(R.id.etPwd), StaticScheme.required(), SchemeUtil.getPwdScheme())
                .add(widgetAccess.findEditText(R.id.etPwdAgain), ValueScheme.equalsTo(lazyLoaders.fromEditText(R.id.etPwd)))
        ;

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(200, ivPwdToggleAgain, () -> {
            isSeeAgain = !isSeeAgain;
            etPwdAgain.setTransformationMethod(isSeeAgain ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggleAgain.setImageResource(isSeeAgain ? R.mipmap.lib_icon_show : R.mipmap.lib_icon_hide);
        });
        ClickUtil.click(200, ivPwdToggle, () -> {
            isSee = !isSee;
            etPwd.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle.setImageResource(isSee ? R.mipmap.lib_icon_show : R.mipmap.lib_icon_hide);

        });
        ClickUtil.click(tvAgreement, () -> {

            ARouter.getInstance().build(RConfig.MINE_NEWS_DETAIL).withString(Constans.WALLET_XIEYI, "reg_agree")
                    .navigation();
        });
        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                if (!checkbox.isChecked()) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_createWalletActivity2_j));
                    return;
                }
                mPresenter.createAccount(isFirst, coinEnum.getId()+"", "1", "", etPwd.getText().toString(), "");
            }
        });
    }

    public void createSuccess(CreateBean data) {
        if (data.getBtc()!=null) {
            WalletBean walletBeanBtc = new WalletBean(data.getBtc().getAddr(), data.getBtc().getPri(), CoinEnum.BTC.getCode(), CoinEnum.BTC.getId(), etName.getText().toString(), etPwd.getText().toString());
            walletBeanBtc.setWords(data.getBtc().getMnemonic());
            walletBeanViewModel.insert(walletBeanBtc);
        }
        if (data.getEth()!=null) {
            WalletBean walletBeanEth = new WalletBean(data.getEth().getAddr(), data.getEth().getPri(), CoinEnum.ETH.getCode(), CoinEnum.ETH.getId(), etName.getText().toString(), etPwd.getText().toString());
            walletBeanEth.setWords(data.getEth().getMnemonic());
            walletBeanEth.setKeystore(data.getEth().getKsJson());
            walletBeanViewModel.insert(walletBeanEth);
        }
        ARouter.getInstance().build(RConfig.WALLET_CREATE_WALLET_SUCCESS).navigation();
        finish();
    }
}
